package com.eedi.framework.user.dal.mapper;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.eedi.framework.user.controller.sys.vo.*;
import com.eedi.framework.common.enums.CommonStatusEnum;
import com.eedi.framework.common.enums.UserTypeEnum;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.framework.mybatis.core.mapper.BaseMapperX;
import com.eedi.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.eedi.framework.user.controller.sys.vo.*;
import com.eedi.framework.user.convert.SysUserConvert;
import com.eedi.framework.user.dal.dataobject.SysUserDO;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

@Mapper
public interface SysUserMapper extends BaseMapperX<SysUserDO> {


    default String getPwdByLoginId(String sysUserLoginId) {
        return selectOne(SysUserDO::getSysUserLoginId, sysUserLoginId).getSysUserPassword();
    }

    default SysUserLoginInfoResp create(SysUserAccountCreateReq req, CommonStatusEnum sysUserAccountStatus) {
        SysUserDO item = BeanUtils.toBean(req, SysUserDO.class);
        item.setSysUserAccountStatus(sysUserAccountStatus);
        item.setSysUserAccountType(UserTypeEnum.MEMBER_SYS);
        item.setSysUserLoginId(req.getSysUserLoginId());
        insert(item);
        return BeanUtils.toBean(item, SysUserLoginInfoResp.class);
    }

    default void updateStatusBySysUserAccountId(String sysUserId, CommonStatusEnum sysUserAccountStatus) {
        SysUserDO item = new SysUserDO();
        item.setSysUserId(sysUserId);
        item.setSysUserAccountStatus(sysUserAccountStatus);
        updateById(item);
    }


    default PageResult<SysUserLoginInfoResp> page(SysUserAccountPageReq req) {
        PageResult<SysUserDO> sysUserAccountDOPageResult = selectPage(req, new LambdaQueryWrapperX<SysUserDO>()
                .likeIfPresent(SysUserDO::getSysUserMobile, req.getSysUserMobile())
                .likeIfPresent(SysUserDO::getSysUserName, req.getSysUserName())
                .likeIfPresent(SysUserDO::getSysUserLoginId,req.getSysUserLoginId())
                .eqIfPresent(SysUserDO::getSysUserAccountStatus,req.getSysUserAccountStatus())
                .orderByDesc(SysUserDO::getId));
        return sysUserAccountDOPageResult.convertPage(SysUserConvert.INSTANCE::convert);
    }

    default PageResult<SysUserLoginInfoResp> page(SysUserAccountPageReq req,Collection<String> sysUserIds) {
        PageResult<SysUserDO> sysUserAccountDOPageResult = selectPage(req, new LambdaQueryWrapperX<SysUserDO>()
                .inIfPresent(SysUserDO::getSysUserId,sysUserIds)
                .likeIfPresent(SysUserDO::getSysUserMobile, req.getSysUserMobile())
                .likeIfPresent(SysUserDO::getSysUserName, req.getSysUserName())
                .likeIfPresent(SysUserDO::getSysUserLoginId,req.getSysUserLoginId())
                .eqIfPresent(SysUserDO::getSysUserAccountStatus,req.getSysUserAccountStatus())
                .orderByDesc(SysUserDO::getId));
        return sysUserAccountDOPageResult.convertPage(SysUserConvert.INSTANCE::convert);
    }

    default void resetPassword(String sysuserId, String password) {
        SysUserDO item = new SysUserDO();
        item.setSysUserId(sysuserId);
        item.setSysUserPassword(password);
        updateById(item);
    }

    default void resetMobile(String sysuserId, String sysUserAccountMobile) {
        SysUserDO item = new SysUserDO();
        item.setSysUserId(sysuserId);
        item.setSysUserMobile(sysUserAccountMobile);
        //仅支持手机号登录
        item.setSysUserLoginId(sysUserAccountMobile);
        updateById(item);
    }


    default SysUserLoginInfoResp selectByLoginId(String sysUserAccountLoginId) {
        LambdaQueryWrapperX<SysUserDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(SysUserDO::getSysUserLoginId, sysUserAccountLoginId)
                .eq(SysUserDO::getSysUserAccountStatus, CommonStatusEnum.ENABLE);
        return SysUserConvert.INSTANCE.convert(selectOne(queryWrapper));

    }

    default SysUserLoginInfoResp selectByMobileAndRole(String sysUserAccountMobile, UserTypeEnum userTypeEnum) {
        LambdaQueryWrapperX<SysUserDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(SysUserDO::getSysUserMobile, sysUserAccountMobile)
                .eq(SysUserDO::getSysUserAccountType, userTypeEnum)
                .eq(SysUserDO::getSysUserAccountStatus, CommonStatusEnum.ENABLE)
                .orderByDesc(SysUserDO::getId);
        return SysUserConvert.INSTANCE.convert(selectOne(queryWrapper));
    }

    default SysUserLoginInfoResp selectBySysUserId(String sysUserId) {
        LambdaQueryWrapperX<SysUserDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(SysUserDO::getSysUserId, sysUserId)
                .eq(SysUserDO::getSysUserAccountStatus, CommonStatusEnum.ENABLE)
                .last("limit 1");
        return SysUserConvert.INSTANCE.convert(selectOne(queryWrapper));
    }

    default List<SysUserLoginInfoResp> selectBySysUserIds(Collection<String> sysUserIds) {
        if (CollUtil.isEmpty(sysUserIds)){
            return List.of();
        }
        LambdaQueryWrapperX<SysUserDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.in(SysUserDO::getSysUserId, sysUserIds);
        return SysUserConvert.INSTANCE.convert(selectList(queryWrapper));
    }

    default SysUserLoginInfoResp selectByAccountIdIgnoreStatus(String sysUserId) {
        LambdaQueryWrapperX<SysUserDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(SysUserDO::getSysUserId, sysUserId);
        return SysUserConvert.INSTANCE.convert(selectOne(queryWrapper));
    }

    default List<SysUserDO> selectListByNicknameLike(String sysUserName) {
        return selectList(new LambdaQueryWrapperX<SysUserDO>()
                .likeIfPresent(SysUserDO::getSysUserName, sysUserName)
                .eq(SysUserDO::getSysUserAccountStatus, CommonStatusEnum.ENABLE));
    }

    default void  updateLoginTime(String sysUserId,String clientId){
        SysUserDO user=new SysUserDO();
        user.setSysUserId(sysUserId)
                .setSysUserAccountLoginIp(clientId)
                .setSysUserAccountLoginDate(LocalDateTime.now());
        updateById(user);
    }

    default void editUserInfo(String sysUserId, SysUserProfileEditReq req){
        if (StrUtil.isEmpty(sysUserId)){
            return;
        }
        SysUserDO user=new SysUserDO();
        user.setSysUserId(sysUserId);
        if (StrUtil.isNotBlank(req.getSysUserName())){
            user.setSysUserName(req.getSysUserName());
        }
        if (StrUtil.isNotBlank(req.getSysUserLoginId())){
            user.setSysUserLoginId(req.getSysUserLoginId());
        }
        updateById(user);
    }

    default void updateSysUserInfo(SysUserUpReq req){
        if (StrUtil.isEmpty(req.getSysUserId())){
            return;
        }
        SysUserDO user=new SysUserDO();
        user.setSysUserId(req.getSysUserId());
        if (StrUtil.isNotBlank(req.getSysUserName())){
            user.setSysUserName(req.getSysUserName());
        }
        if (ObjUtil.isNotNull(req.getSysUserAccountStatus())){
            user.setSysUserAccountStatus(req.getSysUserAccountStatus());
        }
        updateById(user);
    }

    default Long countUser(){
       return selectCount();
    }

}
